import { PEOPLE_TYPE } from '@/config/h5/index.js'
import API from '@/services/h5/product'

export default {
  namespaced: true,
  state: {
    PEOPLE_TYPE,
    contractId: '', //订单id，如果是再修改则有值
    ids: [], //附件id，包含投被保人、受益人身份证和银行卡
    productInfo: {
      productId: '',
      productName: ''
    },
    carDateObj: {
      toubaoDate: '',
      chengbaoDate: '',
      shengxiaoDate: ''
    },
    // 车主信息
    carPeople: {
      carNo: '',
      name: '',
      idType: '1',
      idNo: '',
      sex: '',
      birthday: '',
      mobile: ''
    },
    // 投保人信息
    appPolicyHolderInfo: {
      name: '',
      idType: '1',
      idNo: '',
      sex: '',
      birthday: '',
      mobile: '',
      isOwner: ''
    },
    // 被投保人信息
    appPolicyInsuredList: {
      name: '',
      idType: '1',
      idNo: '',
      sex: '',
      birthday: '',
      mobile: '',
      isOwner: ''
    },
    //车船税信息
    carInsuranceInfo: {
      carTaxType: '',
      carTax: ''
    },
    sexList: [],
    cardList: [],
    riskList: [],
    policyFiles: []
  },
  mutations: {
    //初始化数据
    initData(state) {
      state.carDateObj = {
        toubaoDate: '',
        chengbaoDate: '',
        shengxiaoDate: ''
      }
      state.carPeople = {
        carNo: '',
        name: '',
        idType: '1',
        idNo: '',
        sex: '',
        birthday: '',
        mobile: ''
      }
      state.appPolicyHolderInfo = {
        name: '',
        idType: '1',
        idNo: '',
        sex: '',
        birthday: '',
        mobile: ''
      }
      state.appPolicyInsuredList = {
        name: '',
        idType: '1',
        idNo: '',
        sex: '',
        birthday: '',
        mobile: ''
      }
      state.riskList = []
      state.ids = []

      state.contractId = ''
      state.policyFiles = []
    },
    setObjData(state, [obj, prop, value]) {
      state[obj][prop] = value
    },
    // 设置产品信息
    setProductInfo(state, productInfo) {
      state.productInfo = productInfo
    },
    //设置日期信息
    setCarDateObj(state, [prop, value]) {
      state.carDateObj[prop] = value
    },
    //设置车主信息
    setCarPeople(state, [prop, value]) {
      state.carPeople[prop] = value
    },
    //设置投保人信息
    setAppPolicyHolderInfo(state, [prop, value]) {
      state.appPolicyHolderInfo[prop] = value
    },
    //设置被保险人信息
    setAppPolicyInsuredList(state, [prop, value]) {
      state.appPolicyInsuredList[prop] = value
    },
    //设置订单号id
    setContractId(state, id) {
      state.contractId = id
    },
    //设置险种信息
    setRiskList(state, data) {
      state.riskList = data
    },
    //设置车船税信息
    setCarInsuranceInfo(state, [prop, value]) {
      state.carInsuranceInfo[prop] = value
      // console.log("state.carInsuranceInfo[prop]",state.carInsuranceInfo[prop])
    },
    setCarIds(state, id) {
      state.ids.push(id)
    },
    //
    setInfoObj(state, [prop, obj]) {
      state[prop] = obj
    },
    setPolicyFiles(state, arr) {
      state.policyFiles = arr
    }
  },
  actions: {
    // 获取产品详情信息
    async getProductDetailInfo({ commit, dispatch }, data) {
      const getPolicyFiles = arr => {
        if (arr.length == 0) return []
        let data = []
        arr.forEach(item => {
          data.push({
            url: item.filePath,
            id: item.id
          })
        })
        return data
      }
      if (data.contractId) {
        commit('setContractId', data.contractId)
        const res = await API.getProductDetailInfo(data)
        // console.log('接口返回数据：',res)
        if (res.data && res.data.code == 1000) {
          let productInfo = {
            productId: res.data.data.policyBase.productId,
            productName: res.data.data.policyBase.productName
          }
          commit('setProductInfo', productInfo)
          commit('setCarDateObj', [
            'chengbaoDate',
            res.data.data.policyBase.acceptionDate
          ])
          commit('setCarInsuranceInfo', [
            'carTaxType',
            res.data.data.policyBase.carTaxType
          ])
          commit('setCarInsuranceInfo', [
            'carTax',
            res.data.data.policyBase.carTax
          ])
          commit('setCarDateObj', [
            'toubaoDate',
            res.data.data.policyBase.applicationDate
          ])
          commit('setCarDateObj', [
            'shengxiaoDate',
            res.data.data.policyBase.inceptionDate
          ])
          // for (let i in state.carPeople) {
          //   commit('setCarPeople', [i, res.data.data.policyCar[i]])
          // }
          // for (let i in state.appPolicyHolderInfo) {
          //   commit('setAppPolicyHolderInfo', [i, res.data.data.policyHolder[i]])
          // }
          // for (let i in state.appPolicyInsuredList) {
          //   commit('setAppPolicyInsuredList', [
          //     i,
          //     res.data.data.policyInsureds[0][i]
          //   ])
          // }
          commit('setInfoObj', ['carPeople', res.data.data.policyCar])
          commit('setInfoObj', [
            'appPolicyHolderInfo',
            res.data.data.policyHolder
          ])
          commit('setInfoObj', [
            'appPolicyInsuredList',
            res.data.data.policyInsureds[0]
          ])
          commit('setPolicyFiles', getPolicyFiles(res.data.data.policyFiles))
          await dispatch('getRiskList', [
            res.data.data.policyRisks,
            data.contractId
          ])
        }
      }
    },
    async getRiskList({ commit }, [arr, contractId]) {
      // console.log('获取车险详情信息',[arr, contractId])
      let data = []
      if (arr && arr.length !== 0) {
        // console.log('不为空时',arr)
        let riskRes = await API.getRiskQuery({
          contractId: contractId
        })
        // console.log('riskRes',riskRes)
        arr.map(item => {
          let obj = {
            isShow: false,
            riskId: item.riskId,
            insurancePeriodId: item.insurancePeriodId,
            payPeriodId: item.payPeriodId,
            paymentWay: item.payWayId,
            sumAssured: item.sumAssured,
            premium: item.premium,
            partIds: item.partIds ? item.partIds : [],
            partId: item.partIds ? item.partIds[0] : '',
            carPolicyNo: item.carPolicyNo ? item.carPolicyNo : '',
            riskName: '',
            prFlag: '',
            carTax: item.carTax, //车险金额
            carTaxType: item.carTaxType //车险类型
          }
          data.push(obj)
        })
        //双重循环，将riskName、prFlag加入data中
        // console.log('循环之前的data',data)
        data.forEach(item => {
          riskRes.data.data.forEach(curr => {
              if(item.riskId == curr.riskId) {
                item.riskName = curr.riskName
                item.prFlag = curr.prFlag
              }
          })
        })
      } else {
        let riskRes = await API.getRiskQuery({
          contractId: contractId
        })
        // console.log('调用车险接口：',riskRes)
        if (riskRes.data.code === 1000) {
          riskRes.data.data.forEach(item => {
            let obj = {
              isShow: false,
              riskId: item.riskId,
              riskName: item.riskName,
              insurancePeriodId: item.insurancePeriodId
                ? item.insurancePeriodId
                : '',
              payPeriodId: item.payPeriodId ? item.payPeriodId : '',
              paymentWay: item.paymentWay ? item.paymentWay : '',
              premium: item.premium ? item.premium : '',
              partIds: item.partIds ? item.partIds : '',
              prFlag: item.prFlag,
              partId: item.partId ? item.partId : '',
              carPolicyNo: item.carPolicyNo ? item.carPolicyNo : '',
              sumAssured: 0,
              carTax: '', //车险金额
              carTaxType: '' //车险类型
            }
            data.push(obj)
          })
        }
      }
      commit('setRiskList', data)
    }
  }
}
